﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace NBOW
{
    public partial class ShowFormat : Form
    {
        public ShowFormat()
        {
            InitializeComponent();
        }

        #region members

        ConnectDB connect = new ConnectDB();
        Common commo = new Common();
        string path = "";

        #endregion

        #region Event Handlers

        private void btnChooseFile_Click(object sender, EventArgs e)
        {
            openFilePath.ShowDialog();
            string File = openFilePath.FileName;
            if (string.IsNullOrEmpty(File))
                return;
            txtPath.Text = File;
            if (txtPath.Text != "")
            {
                path = txtPath.Text;
                DataTable dtBuild = connect.getDataFromXLS(path);
                showField(dtBuild);
                btnEnterBug.Enabled = true;
                btnCancel.Enabled = true;
                // show file
                if (dtBuild != null)
                    gvShowFormat.DataSource = dtBuild;
            }
        }
       
        private void btnEnterBug_Click(object sender, EventArgs e)
        {

            #region check update
            string pathBOW = Environment.CurrentDirectory + "\\DataTest\\BagOfWords.txt";
            FileInfo MyFile = new FileInfo(pathBOW);
            if (!System.IO.Directory.Exists(pathBOW))
            {
                int intYear = 0, intMonth = 0, intDay = 0;
                intYear = MyFile.LastWriteTime.Date.Year;
                intMonth = MyFile.LastWriteTime.Date.Month;
                intDay = MyFile.LastWriteTime.Date.Day;
                if (intYear == System.DateTime.Today.Date.Year)
                {
                    if (intMonth == System.DateTime.Today.Date.Month)
                    {
                        if (System.DateTime.Today.Date.Day - intDay >= 20)
                            showUpdate();
                    }
                    else
                    {
                        if (System.DateTime.Today.Date.Month - intMonth == 1)
                        {
                            if (30 - intDay + System.DateTime.Today.Date.Day >= 20)
                                showUpdate();
                        }
                        else showUpdate();
                    }

                }
                else showUpdate();
            }

            #endregion
            if (clbColumns.CheckedItems.Count > 0)
            {
                EnterBug en = new EnterBug();
                var items = clbColumns.Items;
                foreach (object item in clbColumns.CheckedItems)
                {
                    if (en.pub_str_FieldChecked != string.Empty)
                    {
                        en.pub_str_FieldChecked += "_" + item.ToString();
                    }
                    else
                        en.pub_str_FieldChecked = item.ToString();
                }
                en.path = path;
                en.ShowDialog();
            }
            else
            {
                MessageBox.Show("Choose field to check dulicate !");
                return;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        #endregion

        #region Functions

        private void showField(DataTable dtBuild)
        {
            List<int> getColumns = new List<int>();
            getColumns = commo.disField(dtBuild);

            clbColumns.Items.Clear();
            var items = clbColumns.Items;

            if (dtBuild != null)
            {
                foreach (DataColumn item in dtBuild.Columns)
                {
                    // check columns not empty
                    if (getColumns.Contains(item.Ordinal))
                        continue;
                    else
                        items.Add(string.Format("{0}", item));
                }
            }
        }

        private void showUpdate()
        {
            CreateBOW create = new CreateBOW();
            DialogResult dr = new DialogResult();
            dr = DialogResult.No;
            dr = MessageBox.Show("Please update bag of words...", "Note", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dr == DialogResult.Yes)
                create.ShowDialog();
            else showSearchForm();
        }

        private void showSearchForm()
        {
            if (clbColumns.CheckedItems.Count > 0)
            {
                EnterBug en = new EnterBug();
                var items = clbColumns.Items;
                foreach (object item in clbColumns.CheckedItems)
                {
                    if (en.pub_str_FieldChecked != string.Empty)
                    {
                        en.pub_str_FieldChecked += "_" + item.ToString();
                    }
                    else
                        en.pub_str_FieldChecked = item.ToString();
                }
                en.path = path;
                en.ShowDialog();
            }
            else
            {
                MessageBox.Show("Choose field to check dulicate !");
                return;
            }
        }

        #endregion
    }
}
